﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace 注册列表操作Winform
{
    static class Program
    {
        
        const string regListPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion";
        const string folderName = "Run";
        

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Write2RegList();
            Application.DoEvents();
            Application.Run(new Form1());
        }

        #region 写入注册列表
        static public void Write2RegList()
        {
            string keyName = Application.ProductName;
            string valueName = Application.ExecutablePath; //"D:\\testshare\\Debug\\CF30BackGroundSoftware.exe";
            if (GetRegistData(keyName) == "")
            {
                //Console.WriteLine(keyName + " 已存在！");
                //Console.ReadKey();
                //return;
                Console.WriteLine(keyName + " 不存在，准备写入！");
                //不存在
                WTRegedit(keyName, valueName);

                Console.WriteLine(keyName + " 写入完成，下面进行读出，判断写入是否正确！");

                if (GetRegistData(keyName) != "")
                {
                    Console.WriteLine(keyName + " 已写入！");
                }
                else
                {
                    Console.WriteLine(keyName + " 写入不成功！");
                }
            }
        }

        /// <summary>
        /// 写注册列表
        /// </summary>
        /// <param name="regKey"></param>
        /// <param name="regValue"></param>
        /// <param name="startPath">软件的启动路径</param>
        /// <returns></returns>
        static public bool WriteRegList(string regKey, string regValue, string startPath)
        {
            if (GetRegistData(regKey) != "")
            {
                return true;
            }

            //不存在
            WTRegedit(regKey, regValue);

            if (GetRegistData(regKey) != "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //读取指定名称的注册表的值
        static private string GetRegistData(string name)
        {
            string registData = "";
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey(regListPath, true);
            RegistryKey aimdir = software.OpenSubKey(folderName, true);
            try
            {
                registData = aimdir.GetValue(name).ToString();
            }
            catch { }

            return registData;
        }

        ///向注册表中写数据 
        static private void WTRegedit(string name, string tovalue)
        {
            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey software = hklm.OpenSubKey(regListPath, true);
            RegistryKey aimdir = software.CreateSubKey(folderName);
            aimdir.SetValue(name, tovalue);
        }

        ///删除注册表中指定的注册表项 
        static private void DeleteRegist(string name)
        {
            string[] aimnames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey(regListPath, true);
            RegistryKey aimdir = software.OpenSubKey(folderName, true);
            aimnames = aimdir.GetSubKeyNames();
            foreach (string aimKey in aimnames)
            {
                if (aimKey == name)
                    aimdir.DeleteSubKeyTree(name);
            }
        }

        //判断指定注册表项是否存在 ,这个函数有问题，所以用 GetRegistData 函数是否能读出键名来代替
        static private bool IsRegeditExit(string name)
        {
            bool _exit = false;
            string[] subkeyNames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey(regListPath, true);
            RegistryKey aimdir = software.OpenSubKey(folderName, true);
            subkeyNames = aimdir.GetSubKeyNames();
            Console.WriteLine("读出的数组长度：" + subkeyNames.Length);
            foreach (string key in subkeyNames)
            {
                Console.WriteLine("在判断函数里打印读到的键值:" + key);
                if (key == name)
                {
                    _exit = true;
                    return _exit;
                }
            }
            return _exit;
        }
        #endregion

    }
}
